//package com.hk.blog.repository.elasticsearch.impl;
//
//import com.hk.blog.domain.es.EsArticleInfo;
//import com.hk.blog.repository.elasticsearch.custom.CustomEsArticleInfoRepository;
//import com.hk.blog.vo.ArticleListVo;
//import com.hk.commons.util.BeanUtils;
//import com.hk.commons.util.StringUtils;
//import com.hk.core.data.commons.utils.OrderUtils;
//import com.hk.core.page.QueryPage;
//import com.hk.core.page.SimpleQueryPage;
//import com.hk.core.query.QueryModel;
//import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
//import org.elasticsearch.index.query.BoolQueryBuilder;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
//import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.PageRequest;
//import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
//import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Objects;
//import java.util.stream.Collectors;
//
//public class EsArticleInfoRepositoryImpl implements CustomEsArticleInfoRepository {
//
//    @Autowired
//    private ElasticsearchOperations elasticsearchOperations;
//
//    @Override
//    public QueryPage<ArticleListVo> findForPage(QueryModel<EsArticleInfo> queryModel) {
//        var param = queryModel.getParam();
//        var queryBuilder = new NativeSearchQueryBuilder()
//                .withFields("id", "title", "browseNum", "lastUpdateDate")
//                .withPageable(PageRequest.of(queryModel.getStartRowIndex(), queryModel.getPageSize(), OrderUtils.toSort(queryModel.getOrders())));
//        if (null != param) {
//            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//            if (Objects.nonNull(param.getUserId())) {
//                boolQuery.must(QueryBuilders.termQuery("userId", param.getUserId()));
//            }
//            if (Objects.nonNull(param.getCategoryId())) {
//                boolQuery.must(QueryBuilders.termQuery("categoryId", param.getCategoryId()));
//            }
//            queryBuilder.withFilter(boolQuery);
//            if (StringUtils.isNotEmpty(param.getTitle())) {
//                List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>(2);
//                filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("title", param.getTitle()),
//                        ScoreFunctionBuilders.weightFactorFunction(8)));
//                filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("content", param.getTitle()),
//                        ScoreFunctionBuilders.weightFactorFunction(2)));
//                FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
//                filterFunctionBuilders.toArray(builders);
//                FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
//                        .scoreMode(FunctionScoreQuery.ScoreMode.SUM).setMinScore(1);
//                queryBuilder.withQuery(functionScoreQueryBuilder);
//            }
//        }
//        var searchHits = elasticsearchOperations.search(queryBuilder.build(), EsArticleInfo.class);
//        return new SimpleQueryPage<>(queryModel, searchHits.get()
//                .map(item -> {
//                    var data = new ArticleListVo();
//                    BeanUtils.copierCopyNotNullProperties(item.getContent(), data);
//                    return data;
//                })
//                .collect(Collectors.toList()), searchHits.getTotalHits());
//    }
//}
